package com.champrin.replicationofmyblockapix.http;

import static com.champrin.replicationofmyblockapix.http.HTTPUtils.getStringByStream;

import android.provider.BaseColumns;
import android.util.Log;

import com.champrin.replicationofmyblockapix.layout.Layout;
import com.google.gson.Gson;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class FinishRecord {
    public static class PlayerEntry implements BaseColumns {
        public static final String TABLE_NAME = "finish_record";
        public static final String COLUMN_NAME_USERNAME = "username";
        public static final String COLUMN_NAME_LAYOUT_ID = "layout_id";
        public static final String COLUMN_NAME_GAME_FINISH_TIME = "finish_time";
    }

    private String username;

    private Integer layout_id;

    private String finish_time;

    public FinishRecord(String username, Integer layout_id, String finish_time) {
        this.username = username;
        this.layout_id = layout_id;
        this.finish_time = finish_time;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getLayout_id() {
        return layout_id;
    }

    public void setLayout_id(Integer layout_id) {
        this.layout_id = layout_id;
    }

    public String getFinish_time() {
        return finish_time;
    }

    public void setFinish_time(String finish_time) {
        this.finish_time = finish_time;
    }

    @Override
    public String toString() {
        return "FinishRecord{" +
                "username='" + username + '\'' +
                ", layout_id=" + layout_id +
                ", finish_time='" + finish_time + '\'' +
                '}';
    }

    public static void saveResult(int layout_id) {
        new Thread() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                UserResponse userResponse = null;
                String result = null;
                try {
                    String path = "http://10.33.73.41:8080/finish?" + "username=" + Player.globolPlayer.getUsername() + "&layout_id=" + layout_id;
                    Log.v("d", path);
                    URL url = new URL(path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(6000);
                    connection.setReadTimeout(6000);
                    //设置请求方式 GET / POST 一定要大小
                    connection.setRequestMethod("GET");
                    connection.setDoInput(true);
                    connection.setDoOutput(false);
                    connection.connect();
                    int responseCode = connection.getResponseCode();
                    if (responseCode != HttpURLConnection.HTTP_OK) {
                        throw new IOException("HTTP error code" + responseCode);
                    }
                    result = getStringByStream(connection.getInputStream());
                    if (result != null) {
                        userResponse = new Gson().fromJson(result, UserResponse.class);
                        Player player = userResponse.getData();
                        if (player != null) {
                            Player.globolPlayer = player;
                            Log.d("d", Player.globolPlayer.toString());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
